home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / cycles / block-hit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  10.3 KB  |  468 lines

  1.  
  2. #include "cycles.h"
  3.  
  4. #ifndef MIN
  5. #define MIN(x, y)  ((x)<(y)?(x):(y))
  6. #endif
  7. #define SIGN(x)   ( (x)<0.0 ? -1.0 : ((x)>0.0?1.0:0.0) )
  8.  
  9. extern CYCLE *good, bike[CYCLES];
  10. extern float vec[4][2];
  11. extern int used[CYCLES];
  12.  
  13. /* welcome to the function from hell */
  14.  
  15. #define no_HALF_DEBUG
  16. #define no_DEBUG_4
  17.  
  18. #ifdef DEBUG
  19. #define HALF_DEBUG
  20. #define VERY_DEBUG
  21. #define BAD 1
  22. #endif
  23.  
  24. #ifdef HALF_DEBUG
  25. int routine, one, two, three;
  26. #endif
  27.  
  28. /*
  29.  * This routine finds the closest obstacle from (x, z) in
  30.  * direction (in the direction vector) dir
  31.  */
  32. float block(int id, float x, float z, int dir, int *close_id) {
  33.     float min, dist, a, b, c, nose;
  34.     float dist2, a2;
  35.     int i, j, d, e, f, level;
  36.  
  37.     level = bike[id].level;
  38.     *close_id = -1;    /* the trail id we're heading for */
  39.  
  40. #ifdef DEBUG
  41. #ifdef VERY_DEBUG
  42. if (id == good->id) printf("this is GOOD:\n");
  43. #else
  44. if (id == good->id)
  45. #endif
  46.  printf("id %d good (%g, %g) bad (%g, %g)\n",
  47.   id, good->origin.x, good->origin.z, bike[BAD].origin.x, bike[BAD].origin.z);
  48. #endif
  49.  
  50. #ifdef HALF_DEBUG
  51. routine = -1;
  52. one = 0;
  53. two = 0;
  54. three = 0;
  55. #endif
  56.  
  57.     if (vec[dir][0] == 0) {    /* we're looking in a z direction */
  58.     min = DIM - vec[dir][1]*z;
  59.  
  60.     /* collide with "good" trail */
  61.     i = good->trail_ptr;
  62.     do {
  63.         dist = good->trail[i].z - z;
  64.         a = SIGN(dist);
  65.         b = good->trail[i].x;
  66.         i = (i - 1 + TRAIL_LENGTH) % TRAIL_LENGTH;
  67.         if (good->trail[i].level == level) {
  68. #ifdef DEBUG
  69. #ifndef VERY_DEBUG
  70. if (id == good->id)
  71. #endif
  72.  printf("x good ptr %d i %d dist %g\n", good->trail_ptr, i, dist);
  73. #endif
  74.         dist2 = good->trail[i].z - z;
  75.         a2 = SIGN(dist2);
  76.         if (f = (a*a2 < 0.0)) dist = 0.0;
  77.         if ( (a == vec[dir][1] || f) && good->trail[i].level == level && i != good->trail_ptr) {
  78.             c = good->trail[i].x;
  79.             if (b > c) {
  80.             d = (x - CYCLE_WIDTH <= b);
  81.             e = (x + CYCLE_WIDTH >= c);
  82.             }
  83.             else {
  84.             d = (x + CYCLE_WIDTH >= b);
  85.             e = (x - CYCLE_WIDTH <= c);
  86.             }
  87.             if (d && e) {
  88.             dist = ABS(dist);
  89.             if (dist < min) {
  90.                 min = dist;
  91.                 *close_id = good->id;
  92.             }
  93. #ifdef HALF_DEBUG
  94. routine = 0;
  95. one = good->id;
  96. two = good->trail_ptr;
  97. three = i;
  98. #endif
  99.             }
  100.         }
  101.         }
  102.     } while (i != good->trail_ptr && good->trail[i].level != -1);
  103.  
  104.     /* collide with "good" bike and leading trail */
  105.     if (id != good->id && good->level == level) {
  106.         if (good->falling != 0)
  107.         nose = 0.0;
  108.         else
  109.         nose = CYCLE_NOSE;
  110.         i = good->trail_ptr;
  111.         dist = good->trail[i].z - z;
  112.         a = SIGN(dist);
  113.         dist2 = good->origin.z + nose*vec[good->vec_ptr][1] - z;
  114.         a2 = SIGN(dist2);
  115.         if (f = (a*a2 < 0.0)) dist = 0.0;
  116.         if (a == vec[dir][1] || f) {
  117.         dist = MIN(a*dist, a2*dist2);
  118.         b = good->trail[i].x;
  119.         c = good->origin.x;
  120.         if (b > c) {
  121.             d = (x - CYCLE_WIDTH <= b);
  122.             e = (x + CYCLE_WIDTH >= c);
  123.         }
  124.         else {
  125.             d = (x + CYCLE_WIDTH >= b);
  126.             e = (x - CYCLE_WIDTH <= c);
  127.         }
  128.         if (d && e) {
  129.             dist = ABS(dist);
  130.             if (dist < min) {
  131.             min = dist;
  132.             *close_id = good->id;
  133.             }
  134. #ifdef HALF_DEBUG
  135. routine = 1;
  136. one = good->id;
  137. two = good->trail_ptr;
  138. three = i;
  139. #endif
  140.         }
  141.         }
  142.     }
  143. #ifdef DEBUG
  144. #ifndef VERY_DEBUG
  145. if (id == good->id)
  146. #endif
  147.  printf("x end of good: dist %g\n", dist);
  148. #endif
  149.  
  150.     /* collide with "bad" trail */
  151.     for (j = 0; j < CYCLES; j++) {
  152.         if (used[j] && j != good->id && bike[j].alive && bike[j].falling >= 0) {
  153.         i = bike[j].trail_ptr;
  154.         do {
  155.             dist = bike[j].trail[i].z - z;
  156.             a = SIGN(dist);
  157.             b = bike[j].trail[i].x;
  158.             i = (i - 1 + TRAIL_LENGTH) % TRAIL_LENGTH;
  159.             if (bike[j].trail[i].level == level) {
  160. #ifdef DEBUG
  161. #ifndef VERY_DEBUG
  162. if (id == good->id)
  163. #endif
  164.  printf("x bad  i %d dist %g\n", i, dist);
  165. #endif
  166.             dist2 = bike[j].trail[i].z - z;
  167.             a2 = SIGN(dist2);
  168.             if (f = (a*a2 < 0.0)) dist = 0.0;
  169.             if ( (a == vec[dir][1] || f) && bike[j].trail[i].level == level && i != bike[j].trail_ptr) {
  170.                 c = bike[j].trail[i].x;
  171.                 if (b > c) {
  172.                 d = (x - CYCLE_WIDTH <= b);
  173.                 e = (x + CYCLE_WIDTH >= c);
  174.                 }
  175.                 else {
  176.                 d = (x + CYCLE_WIDTH >= b);
  177.                 e = (x - CYCLE_WIDTH <= c);
  178.                 }
  179.                 if (d && e) {
  180.                 dist = ABS(dist);
  181.                 if (dist < min) {
  182.                     min = dist;
  183.                     *close_id = bike[j].id;
  184.                 }
  185. #ifdef HALF_DEBUG
  186. routine = 2;
  187. one = bike[j].id;
  188. two = bike[j].trail_ptr;
  189. three = i;
  190. #endif
  191.                 }
  192.             }
  193.             }
  194.         } while (i != bike[j].trail_ptr && bike[j].trail[i].level != -1);
  195.  
  196. #ifdef DEBUG
  197. #ifndef VERY_DEBUG
  198. if (id == good->id)
  199. #endif
  200.  printf("x bad pre last seg: dist %g\n", dist);
  201. #endif
  202.         /* collide with "bad" bike and leading trail */
  203.         if (id != bike[j].id && bike[j].level == level) {
  204.             if (bike[j].falling != 0)
  205.             nose = 0.0;
  206.             else
  207.             nose = CYCLE_NOSE;
  208.             i = bike[j].trail_ptr;
  209.             dist = bike[j].trail[i].z - z;
  210.             a = SIGN(dist);
  211.             dist2 = bike[j].origin.z + nose*vec[bike[j].vec_ptr][1] - z;
  212.             a2 = SIGN(dist2);
  213.             if (f = (a*a2 < 0.0)) dist = 0.0;
  214.             if (a == vec[dir][1] || f) {
  215.             dist = MIN(a*dist, a2*dist2);
  216.             b = bike[j].trail[i].x;
  217.             c = bike[j].origin.x + nose*vec[bike[j].vec_ptr][0];
  218.             if (b > c) {
  219.                 d = (x - CYCLE_WIDTH <= b);
  220.                 e = (x + CYCLE_WIDTH >= c);
  221.             }
  222.             else {
  223.                 d = (x + CYCLE_WIDTH >= b);
  224.                 e = (x - CYCLE_WIDTH <= c);
  225.             }
  226.             if (d && e) {
  227.                 dist = ABS(dist);
  228.                 if (dist < min) {
  229.                 min = dist;
  230.                 *close_id = bike[j].id;
  231.                 }
  232. #ifdef HALF_DEBUG
  233. routine = 3;
  234. one = bike[j].id;
  235. two = bike[j].trail_ptr;
  236. three = i;
  237. #endif
  238.             }
  239.             }
  240.         }
  241.         }
  242.     }
  243.     }
  244.     else {    /* we're looking in an x direction */
  245.     min = DIM - vec[dir][0]*x;
  246.  
  247.     /* collide with "good" trail */
  248.     i = good->trail_ptr;
  249.     do {
  250.         dist = good->trail[i].x - x;
  251.         a = SIGN(dist);
  252.         b = good->trail[i].z;
  253.         i = (i - 1 + TRAIL_LENGTH) % TRAIL_LENGTH;
  254.         if (good->trail[i].level == level) {
  255. #ifdef DEBUG
  256. #ifndef VERY_DEBUG
  257. if (id == good->id)
  258. #endif
  259.  printf("z good i %d dist %g\n", i, dist);
  260. #endif
  261.         dist2 = good->trail[i].x - x;
  262.         a2 = SIGN(dist2);
  263.         if (f = (a*a2 < 0.0)) dist = 0.0;
  264.  
  265. #ifdef DEBUG_4
  266. if (id == good->id) printf("in4: tp %d, i %d (i level %d) dist %g a %g (dir %g), b %g\n",
  267.  good->trail_ptr, i, good->trail[i].level, dist, a, vec[dir][0], b);
  268. #endif
  269.         if ( (a == vec[dir][0] || f) && good->trail[i].level == level && i != good->trail_ptr) {
  270.             c = good->trail[i].z;
  271.             if (b > c) {
  272.             d = (z - CYCLE_WIDTH <= b);
  273.             e = (z + CYCLE_WIDTH >= c);
  274.             }
  275.             else {
  276.             d = (z + CYCLE_WIDTH >= b);
  277.             e = (z - CYCLE_WIDTH <= c);
  278.             }
  279. #ifdef DEBUG_4
  280. if (id == good->id) printf("in4:   c %g, d %d, e %d\n", c, d, e);
  281. #endif
  282.             if (d && e) {
  283.             dist = ABS(dist);
  284.             if (dist < min) {
  285.                 min = dist;
  286.                 *close_id = good->id;
  287.             }
  288.  
  289. #ifdef DEBUG_4
  290. if (id == good->id) printf("in4:      dist %g\n", dist);
  291. #endif
  292.  
  293. #ifdef HALF_DEBUG
  294. routine = 4;
  295. one = good->id;
  296. two = good->trail_ptr;
  297. three = i;
  298. #endif
  299.             }
  300.         }
  301.         }
  302.     } while (i != good->trail_ptr && good->trail[i].level != -1);
  303.  
  304.     /* collide with "good" bike and leading trail */
  305.     if (id != good->id && good->level == level) {
  306.         if (good->falling != 0)
  307.         nose = 0.0;
  308.         else
  309.         nose = CYCLE_NOSE;
  310.         i = good->trail_ptr;
  311.         dist = good->trail[i].x - x;
  312.         a = SIGN(dist);
  313.         dist2 = good->origin.x + nose*vec[good->vec_ptr][0] - x;
  314.         a2 = SIGN(dist2);
  315.         if (f = (a*a2 < 0.0)) dist = 0.0;
  316.         if (a == vec[dir][0] || f) {
  317.         dist = MIN(a*dist, a2*dist2);
  318.         b = good->trail[i].z;
  319.         c = good->origin.z;
  320.         if (b > c) {
  321.             d = (z - CYCLE_WIDTH <= b);
  322.             e = (z + CYCLE_WIDTH >= c);
  323.         }
  324.         else {
  325.             d = (z + CYCLE_WIDTH >= b);
  326.             e = (z - CYCLE_WIDTH <= c);
  327.         }
  328.         if (d && e) {
  329.             dist = ABS(dist);
  330.             if (dist < min) {
  331.             min = dist;
  332.             *close_id = good->id;
  333.             }
  334. #ifdef HALF_DEBUG
  335. routine = 5;
  336. one = good->id;
  337. two = good->trail_ptr;
  338. three = i;
  339. #endif
  340.         }
  341.         }
  342.     }
  343.  
  344. #ifdef DEBUG
  345. #ifndef VERY_DEBUG
  346. if (id == good->id)
  347. #endif
  348.  printf("z end of good: dist %g\n", dist);
  349. #endif
  350.  
  351.     /* collide with "bad" trail */
  352.     for (j = 0; j < CYCLES; j++) {
  353.         if (used[j] && j != good->id && bike[j].alive && bike[j].falling >= 0) {
  354.         i = bike[j].trail_ptr;
  355.         do {
  356.             dist = bike[j].trail[i].x - x;
  357.             a = SIGN(dist);
  358.             b = bike[j].trail[i].z;
  359.             i = (i - 1 + TRAIL_LENGTH) % TRAIL_LENGTH;
  360.             if (bike[j].trail[i].level == level) {
  361. #ifdef DEBUG
  362. #ifndef VERY_DEBUG
  363. if (id == good->id)
  364. #endif
  365.  printf("z bad  i %d dist %g\n", i, dist);
  366. #endif
  367.             dist2 = bike[j].trail[i].x - x;
  368.             a2 = SIGN(dist2);
  369.             if (f = (a*a2 < 0.0)) dist = 0.0;
  370.             if ( (a == vec[dir][0] || f) && bike[j].trail[i].level == level && i != bike[j].trail_ptr) {
  371.                 c = bike[j].trail[i].z;
  372.                 if (b > c) {
  373.                 d = (z - CYCLE_WIDTH <= b);
  374.                 e = (z + CYCLE_WIDTH >= c);
  375.                 }
  376.                 else {
  377.                 d = (z + CYCLE_WIDTH >= b);
  378.                 e = (z - CYCLE_WIDTH <= c);
  379.                 }
  380.                 if (d && e) {
  381.                 dist = ABS(dist);
  382.                 if (dist < min) {
  383.                     min = dist;
  384.                     *close_id = bike[j].id;
  385.                 }
  386. #ifdef HALF_DEBUG
  387. routine = 6;
  388. one = bike[j].id;
  389. two = bike[j].trail_ptr;
  390. three = i;
  391. #endif
  392.                 }
  393.             }
  394.             }
  395.         } while (i != bike[j].trail_ptr && bike[j].trail[i].level != -1);
  396.  
  397. #ifdef DEBUG
  398. #ifndef VERY_DEBUG
  399. if (id == good->id)
  400. #endif
  401.  printf("z bad pre last seg: dist %g\n", dist);
  402. #endif
  403.         /* collide with "bad" bike and leading trail */
  404.         if (id != bike[j].id && bike[j].level == level) {
  405.             if (bike[j].falling != 0)
  406.             nose = 0.0;
  407.             else
  408.             nose = CYCLE_NOSE;
  409.             i = bike[j].trail_ptr;
  410.             dist = bike[j].trail[i].x - x;
  411.             a = SIGN(dist);
  412.             dist2 = bike[j].origin.x + nose*vec[bike[j].vec_ptr][0] - x;
  413.             a2 = SIGN(dist2);
  414.             if (f = (a*a2 < 0.0)) dist = 0.0;
  415.             if (a == vec[dir][0] || f) {
  416.             dist = MIN(a*dist, a2*dist2);
  417.             b = bike[j].trail[i].z;
  418.             c = bike[j].origin.z + nose*vec[bike[j].vec_ptr][1];
  419.             if (b > c) {
  420.                 d = (z - CYCLE_WIDTH <= b);
  421.                 e = (z + CYCLE_WIDTH >= c);
  422.             }
  423.             else {
  424.                 d = (z + CYCLE_WIDTH >= b);
  425.                 e = (z - CYCLE_WIDTH <= c);
  426.             }
  427.             if (d && e) {
  428.                 dist = ABS(dist);
  429.                 if (dist < min) {
  430.                 min = dist;
  431.                 *close_id = bike[j].id;
  432.                 }
  433. #ifdef HALF_DEBUG
  434. routine = 7;
  435. one = bike[j].id;
  436. two = bike[j].trail_ptr;
  437. three = i;
  438. #endif
  439.             }
  440.             }
  441.         }
  442.         }
  443.     }
  444.     }
  445. #ifdef DEBUG
  446. #ifndef VERY_DEBUG
  447. if (id == good->id)
  448. #endif
  449.  printf("min %g\n", min);
  450. #endif
  451.  
  452. #ifdef HALF_DEBUG
  453. #ifndef VERY_DEBUG
  454. if (id == good->id)
  455. #endif
  456.  {
  457.   printf("id %d good (%g, %g) dir (%g, %g) trails %d\n",
  458.    id, good->origin.x, good->origin.z,
  459.    good->direction.x, good->direction.z, good->trail_ptr);
  460.   printf("routine %d\n", routine);
  461.   printf("bike %d headtrail %d trail %d\n", one, two, three);
  462.   printf("min %g\n", min);
  463. }
  464. #endif
  465.  
  466.     return min;
  467. }
  468.